﻿// JScript 文件

    //当前页索引
    var varPageIndex;
    
    //总页数
    var varPageCount;
    
    //页大小
    var varPageSize=6;
    
    //商品类型
    var varProductType;

//获取总页数的回调函数
function PostRequestDataCount(URL,data)
{ 
    var xmlhttp = createXmlHttpRequest(); 

    xmlhttp.Open("POST",URL, false); 

    xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); 

    xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/GetProductCoutByProductTyppe"); 

    try 
    { 
        xmlhttp.Send(data); 

        var result = xmlhttp.status; 
    } 

    catch(ex)
    { 
    } 

    if(result==200)
     { 

         var varTempCount=xmlhttp.responseXML.text;
         
         if(varTempCount%varPageSize==0)
         {
             varPageCount=varTempCount/varPageSize;
         }
         else
         {
             varPageCount=(varTempCount/varPageSize)+1;
         }
         
         //填充下拉列表
         AddSelect(varPageCount);

    } 

    xmlhttp = null; 

} 

//或总的页数
function GetPageCount()
{ 

    var url = 'http://192.168.1.66/WSofSmartBusiness/ProductService.asmx'; 
    
    varProductType=document.getElementById("productType").value;
    
    var data ; 

    data = '<?xml version="1.0" encoding="utf-8"?>'; 

    data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; 

    data = data + '<soap:Body>'; 

    data = data + '<GetProductCoutByProductTyppe xmlns="http://tempuri.org/">'; 
    
    data =data +'<productType>'+varProductType+'</productType>';

    data = data + '</GetProductCoutByProductTyppe>'; 

    data = data + '</soap:Body>'; 

    data = data + '</soap:Envelope>'; 
    
    PostRequestDataCount(url,data);

}

//当前页的列表数回调函数
function PostRequestDataList(URL,data)
{ 
 //    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    
    var xmlhttp=createXmlHttpRequest();

    xmlhttp.Open("POST",URL, false); 

    xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); 

    xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/GetProductListByProductType"); 

    try 
    { 
        xmlhttp.Send(data); 

        var result = xmlhttp.status; 
    } 

    catch(ex)
    { 
    } 

    if(result==200)
     {
        //显示列表值
         Display(xmlhttp.responseXML);
    } 

    xmlhttp = null; 

} 

//获取当前页列表
function GetPageList(pageSize,pageIndex)
{ 

    //当前页的索引
    varPageIndex=pageIndex;
     
    var url = 'http://192.168.1.66/WSofSmartBusiness/ProductService.asmx'; 

    var data ; 

    data = '<?xml version="1.0" encoding="utf-8"?>'; 

    data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; 

    data = data + '<soap:Body>'; 

    data = data + '<GetProductListByProductType xmlns="http://tempuri.org/">'; 

    data = data + '<iPageSize>'+pageSize+'</iPageSize>';
    
    data = data+'<iPageIndex>'+pageIndex+'</iPageIndex>'; 
    
    data = data+'<productType>'+varProductType+'</productType>';

    data = data + '</GetProductListByProductType>'; 

    data = data + '</soap:Body>'; 

    data = data + '</soap:Envelope>'; 
    
    PostRequestDataList(url,data);

}

 
//填充下拉列表
function AddSelect(result)
{
    for(var i=1;i<=result;i++)
    {
        document.all("selectIndex").options.add(new Option(i,i));
    }
}

//下拉列表框中的值更改时触发的事件
//参数为当前选择项的值
function PageIndex(index)
{
    GetPageList(varPageSize,index);
}


//填充表格
function Display(result)
{
    //获取结果xml文档
    var table=result.getElementsByTagName("ds");
    
    //先清空DIV
    document.getElementById("divResult").innerHTML="";
    
   //循环读取每一个标签
    for(var i = 0; i < table.length; i++)   
    {
        var tempobj, id, name, price;

        tempobj = table[i].getElementsByTagName("ProductID");
        
        id =tempobj[0].childNodes[0].nodeValue;

        tempobj = table[i].getElementsByTagName("Name");

        name =tempobj[0].childNodes[0].nodeValue;

        tempobj = table[i].getElementsByTagName("SalesPrice");

        price =tempobj[0].childNodes[0].nodeValue;
         
        var row=creatTable(id,name,price)+"<HR>";
        
        //填充DIV
        document.getElementById("divResult").innerHTML+=row;

//        document.getElementById("result").innerHTML += row;  
     }     
    
    //绑定必要的其他信息
    BindOtherInfo();
    
}

//绑定其他信息，如当前页，总页数，分页按钮等
function BindOtherInfo()
{
   //显示当前页
    document.getElementById("txtPageIndex").value=varPageIndex;
    
    //当前下拉列表框的值
    document.all("selectIndex").selectedIndex=varPageIndex;
    
    //当前页不是第一页,显示上一页的按钮
    if(varPageIndex>1)
    {
      pre.style.display="";
    }
    else
    {
       pre.style.display="none";
    }
    
    //当前页不是最后一页,显示下一页的按钮
    if(varPageIndex<varPageCount)
    {
        next.style.display="";
    }
   else
   {
       next.style.display="none";
   }
}

//首页
function First()
{
    GetPageList(varPageSize,1);
}

//末页
function End()
{
    GetPageList(varPageSize,varPageCount);
}

//上一页
function Pre()
{
   if(varPageIndex>1)
   {
     //当前大于第一页
      pre.style.display="";
      var index=varPageIndex-1;
      GetPageList(varPageSize,index);
   }
}

//下一页
function Next()
{
   if(varPageIndex<varPageCount)
   {
        //当前页小于最后一页
        next.style.display="";
        var index=varPageIndex+1;
        GetPageList(varPageSize,index);
   }
}

//创建表格
function creatTable(productID,name,price)
{
    var builder="";
    builder=builder+"<table style='width: 100%; height: 100%'>";
    builder=builder+"<tr><td rowspan='2' style='width: 120px; height: 80px;'>";
    builder=builder+"<img style='width: 120px; height: 80px' onclick=window.open('ProductInfo.aspx?productID="+productID+"') src='ImagInfo.aspx?productID="+productID+"'></td>";
    builder=builder+"<td style='width: 200px;text-align:left'>名称：";            
    builder=builder+"<a href='ProductInfo.aspx?productID="+productID+"'>"+name+"</a></td>";                
    builder=builder+"<td style='width: 280px'></td></tr><tr>";
    builder=builder+"<td style='width: 200px; text-align:left'>价格：";            
    builder=builder+price;                
    builder=builder+"</td><td style='width: 280px' align='right'>";
    builder=builder+"<a href='ManageCart.aspx?productID="+productID+"'>加入购物车</a>";                
    builder=builder+"</td></tr></table>";            
    
    return builder;
}

